import re
from datetime import datetime
import time
from bidspider.settings import LAST_CRAWL_TIME
import uuid


class TextUtils:
    @classmethod
    def format_content(cls, content, response):
        # 格式化内容：去掉html(除img,a)，替换为p,并格式化img和a标签
        pattern = r'<(p|div|br)[^>]*>'
        content = re.sub(pattern, '\n', content)
        pattern = r'<(?!img)(?!a)(?!/a)[^>]+>'
        content = re.sub(pattern, '', content)
        content = re.sub('\n', '</p><p>', content)
        content = '<p>' + content + '</p>'
        content = re.sub('<p>\\s*</p>', '', content)
        # 清理img和a标签的 其他属性 以及 格式化链接
        content = cls.format_img(content, response)
        content = cls.format_a(content, response)
        return content

    @classmethod
    def format_a(cls, content, response):
        pattern = r'<a[^>]+>'
        a_tags = re.findall(pattern, content)
        for a_tag in a_tags:
            href_index = a_tag.find("href=")
            if href_index != -1:
                src_1 = a_tag[href_index + 6:]
                if a_tag[href_index + 5:href_index + 6] == '"':
                    src_index_end = src_1.index('"')
                else:
                    src_index_end = src_1.index('\'')
                if src_index_end > -1:
                    src = src_1[:src_index_end]
                    new_a = "<a href=\"" + response.urljoin(src) + "\">"
                    content = content.replace(a_tag, new_a)
            else:
                content = content.replace(a_tag, "<a href=\"javascript:;\">")
        return content

    @classmethod
    def format_img(cls, content, response):
        pattern = r'<img[^>]+>'
        imgs = re.findall(pattern, content)
        for img in imgs:
            src_index = img.find("src=")
            if src_index != -1:
                src_1 = img[src_index + 5:]
                if img[src_index + 4:src_index + 5] == '"':
                    src_index_end = src_1.index('"')
                else:
                    src_index_end = src_1.index('\'')
                if src_index_end > -1:
                    src = src_1[:src_index_end]
                    new_img = "<img src=\"" + response.urljoin(src) + "\">"
                    content = content.replace(img, new_img)
            else:
                content = content.replace(img, '')
        return content

    @classmethod
    def data_time_is_available(cls, data_time, date_format):
        md = datetime.strptime(data_time, date_format)
        ld = datetime.strptime(LAST_CRAWL_TIME, '%Y-%m-%d %H:%M:%S')
        return md > ld

    @classmethod
    def get_current_datetime_str(cls):
        return datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    @classmethod
    def get_time_id(cls):
        return str(int(time.time()))

    @classmethod
    def get_uuid(cls):
        return str(uuid.uuid4()).replace('-', '')

    @classmethod
    def get(cls, item, key):
        if key in item:
            return item[key]
        else:
            return ''
